package com.hw.paltfrom.service.system;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hw.paltfrom.mapper.HwBaseAreaMapper;
import com.hw.paltfrom.pojo.HwBaseArea;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.LanguageUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.BaseAreaVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
/**
 *行政区域
 * 崔孟超
 * 2019年11月6日15:08:14
 */
@Service
public class BaseAreaServer {

	@Autowired
	private HwBaseAreaMapper hwBaseAreaMapper;

	/**
	 * 行政区域添加
	 * @param vo
	 * @return
	 */
	public Map<String, Object> insert(BaseAreaVo vo) {
         String fParentId=vo.getfParentid();
		 String fAreaCode=vo.getfAreacode();
         String fAreaName=vo.getfAreaname();
         Integer fLayer=vo.getfLayer();
         Integer fSortCode =vo.getfSortcode();
         Integer fDeleteMark=vo.getfDeletemark();
         Integer fEnabledMark=vo.getfEnabledmark();
         String fDescription =vo.getfDescription();
		 String fCreateUserId=vo.getfCreateuserid();
		 String fCreateUserName=vo.getfCreateusername();
		 
		 
		 
		// 先判断分类名称 或  编码是否重复
		Wrapper<HwBaseArea> namewrapper = new EntityWrapper<HwBaseArea>();
		namewrapper.eq("F_AreaName", fAreaName);
		List<HwBaseArea> hwBaseAreasList = hwBaseAreaMapper.selectList(namewrapper);

		if (!hwBaseAreasList.isEmpty()) {
			return ResponeResultUtil.fail(HWSystemCode.AREANAME_EXIT);
		}
		Wrapper<HwBaseArea> codewrapper = new EntityWrapper<HwBaseArea>();
		codewrapper.eq("F_AreaCode", fAreaCode);
		List<HwBaseArea> codeList = hwBaseAreaMapper.selectList(codewrapper);
		if (!codeList.isEmpty()) {
			return ResponeResultUtil.fail(HWSystemCode.AREACODE_EXIT);
		}
        //生成ID、创建时间、插入数据库
		HwBaseArea hwBaseArea = new HwBaseArea();
		hwBaseArea.setfAreaid(fAreaCode);
		hwBaseArea.setfParentid(fParentId);
		hwBaseArea.setfAreacode(fAreaCode);
		hwBaseArea.setfAreaname(fAreaName);
		hwBaseArea.setfQuickquery(LanguageUtil.toHanyuPinyin(fAreaName));
        hwBaseArea.setfSimplespelling(LanguageUtil.getAllFirstLetter(fAreaName));
        hwBaseArea.setfLayer(fLayer);
        hwBaseArea.setfSortcode(fSortCode);
		hwBaseArea.setfDeletemark(fDeleteMark);
		hwBaseArea.setfEnabledmark(fEnabledMark);
		hwBaseArea.setfDescription(fDescription);
		hwBaseArea.setfCreatedate(new Timestamp(System.currentTimeMillis()));
		hwBaseArea.setfCreateuserid(fCreateUserId);
		hwBaseArea.setfCreateusername(fCreateUserName);
		Integer insert = hwBaseAreaMapper.insert(hwBaseArea);
		if (insert != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);

	}

	/**
	 * 行政区域查询
	 * @param vo
	 * @return
	 */
	public Map<String, Object> queryAllList(BaseAreaVo vo) {
		String itemcode = vo.getfAreacode();
		String itemname = vo.getfAreaname();
		String fAreaid = vo.getfAreaid();
         //模糊查询规则
		Wrapper<HwBaseArea> namewrapper = new EntityWrapper<HwBaseArea>();
		// 查全市 fAreaid
		if (StringUtils.isNotBlank(fAreaid)) {
			namewrapper.eq("F_AreaId", fAreaid).eq("F_DeleteMark", "0");
		}
		if (StringUtils.isNotBlank(itemcode)) {
			namewrapper.like("F_AreaCode", itemcode).and().eq("F_DeleteMark", "0");//.orderBy("F_SortCode");
		}
		if (StringUtils.isNotBlank(itemname)) {
			namewrapper.like("F_AreaName", itemname).and().eq("F_DeleteMark" ,"0");//.orderBy("F_SortCode");
		}
		namewrapper.eq("F_DeleteMark" ,"0").orderBy("F_SortCode");
		List<HwBaseArea> hwBaseAreasList = hwBaseAreaMapper.selectList(namewrapper);
		JSONArray array = JSONArray.parseArray(JSON.toJSONString(hwBaseAreasList));
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, array);
	}

	/**
	 * 行政区域更新
	 * @param vo
	 * @return
	 */
	public Map<String, Object> update(BaseAreaVo vo) {
		String fAreaCode=vo.getfAreacode();
		Integer integer = vo.getfEnabledmark();
		String fAreaName=vo.getfAreaname();
		Integer fSortCode =vo.getfSortcode();
		Integer fDeleteMark=vo.getfDeletemark();
		String fDescription =vo.getfDescription();
		String fModifyUserId=vo.getfModifyuserid();
		String fModifyUserName=vo.getfModifyusername();
		// 通过ID查询待更新的数据
		Wrapper<HwBaseArea> wrapper = new EntityWrapper<HwBaseArea>();
		wrapper.eq("F_AreaCode", fAreaCode);
		HwBaseArea hwBaseArea = new HwBaseArea();
        //如果删除标记为1的话，当前是删除操作
		if(vo.getfDeletemark()==1){
			hwBaseArea.setfDeletemark(fDeleteMark);
            //记录是谁删除的
			hwBaseArea.setfModifydate(new Timestamp(System.currentTimeMillis()));
			hwBaseArea.setfModifyuserid(vo.getfModifyuserid());
			hwBaseArea.setfModifyusername(vo.getfModifyusername());
			Integer update = hwBaseAreaMapper.update(hwBaseArea, wrapper);
			if (update != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
		}else{//更新数据库
			Wrapper<HwBaseArea> codewrapper = new EntityWrapper<HwBaseArea>();
			codewrapper.eq("F_AreaCode", fAreaCode).notIn("F_AreaId", vo.getfAreaid());
			List<HwBaseArea> codeList = hwBaseAreaMapper.selectList(codewrapper);
			if (!codeList.isEmpty()) {
				return ResponeResultUtil.fail(HWSystemCode.AREACODE_EXIT);
			}

			Wrapper<HwBaseArea> namewrapper = new EntityWrapper<HwBaseArea>();
			namewrapper.eq("F_AreaName", vo.getfAreaname()).notIn("F_AreaId", vo.getfAreaid());//.and().eq("F_AreaCode",fAreaCode)
			List<HwBaseArea> HwBaseRolesList = hwBaseAreaMapper.selectList(namewrapper);
			if (!HwBaseRolesList.isEmpty()) {
				return ResponeResultUtil.fail(HWSystemCode.AREANAME_EXIT);
			}
			hwBaseArea.setfAreaname(fAreaName);
			hwBaseArea.setfAreacode(fAreaCode);
			hwBaseArea.setfSortcode(fSortCode);
			hwBaseArea.setfDeletemark(fDeleteMark);
			if ("".equals(fDescription)){
				hwBaseArea.setfDescription("");
			}else {
				hwBaseArea.setfDescription(vo.getfDescription());
			}
			hwBaseArea.setfModifydate(new Timestamp(System.currentTimeMillis()));
			hwBaseArea.setfModifyuserid(fModifyUserId);
			hwBaseArea.setfEnabledmark(integer);
			hwBaseArea.setfQuickquery(LanguageUtil.toHanyuPinyin(fAreaName));
			hwBaseArea.setfSimplespelling(LanguageUtil.getAllFirstLetter(fAreaName));
			hwBaseArea.setfModifyusername(fModifyUserName);
			Wrapper<HwBaseArea> idwrapper = new EntityWrapper<HwBaseArea>();
			idwrapper.eq("F_AreaId",vo.getfAreaid());
			Integer update = hwBaseAreaMapper.update(hwBaseArea, idwrapper);
			if (update != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}



	/**
	 * //修改状态
	 * @param vo
	 * @return
	 */
	public Map<String, Object> userState(BaseAreaVo vo) {
		//F_AreaId 区域主键(GUID)
		String fAreaid = vo.getfAreaid();
		//F_EnabledMark 状态
		Integer integer = vo.getfEnabledmark();
		Wrapper<HwBaseArea> namewrapper = new EntityWrapper<HwBaseArea>();
		namewrapper.eq("F_AreaId",fAreaid);
		HwBaseArea hwBaseArea=new HwBaseArea();
		hwBaseArea.setfAreaid(fAreaid);
		hwBaseArea.setfEnabledmark(integer);
		Integer update = hwBaseAreaMapper.update(hwBaseArea, namewrapper);
		if (update!=1){
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	   }
	}

